package cn.com.dycc.warning_info.info_user.service;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import cn.com.dycc.sys_manage.usersysmanagement.dao.UserDao;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.Paging;
import cn.com.dycc.warning_info.info_user.dao.InfoUserDao;
import cn.com.dycc.warning_info.info_user.entity.InfoUser;
import cn.com.dycc.warning_info.info_user.param.InfoUserParam;

/**
 * 
 * Description: 提醒通知信息管理业务层实现
 * Author: Liket
 * Version: 1.0
 * Create Date Time: 2018年11月19日 上午10:16:03.
 * Update Date Time: 
 * @see
 */
@Service("infoUserService")
public class InfoUserServiceImpl implements InfoUserService{
	
	@Resource
	private InfoUserDao infoUserDao;
	
	@Resource
	private UserDao userDao;
	
	@Resource(name="paging")
	private Paging paging;
	
		
	public int addInfoUser(HttpServletRequest req, InfoUser infoUser)
			throws InfoUserNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		String user_id = (String)session.getAttribute("id");
		if(org_id==null){
			throw new InfoUserNotFoundException("请重新登录");
		}
		if(infoUser==null){
			throw new InfoUserNotFoundException("请填写信息");
		}
				
		if(infoUser.getUser_id()==null||infoUser.getUser_id().trim().isEmpty()){
			throw new InfoUserNotFoundException("请选择用户");
		}
		
		if(infoUser.getUser_name()==null||infoUser.getUser_name().trim().isEmpty()){
			throw new InfoUserNotFoundException("请选择用户名称");
		}
		
		String account = userDao.findAccountByUserId(infoUser.getUser_id(),org_id);
		if(account==null||account.trim().isEmpty()){
			throw new InfoUserNotFoundException("该员工不存在");
		}
		
		if(infoUser.getInfo_flag()==null||infoUser.getInfo_flag().trim().isEmpty()){
			throw new InfoUserNotFoundException("请选择提醒类别");
		}
		if(infoUser.getInfo_title()==null||infoUser.getInfo_title().trim().isEmpty()){
			throw new InfoUserNotFoundException("请选择提醒标题");
		}
		if(infoUser.getWarning_des()==null||infoUser.getWarning_des().trim().isEmpty()){
			throw new InfoUserNotFoundException("请填写到期提醒内容");
		}
		infoUser.setOrg_id(org_id);
		infoUser.setCreate_date(DateUtil.getNowDate(DateUtil.TIME_FMT));
		infoUser.setOpt_user_id(user_id);
		
		int n = infoUserDao.addInfoUser(infoUser);
		if(n!=1){
			throw new InfoUserNotFoundException("系统繁忙，添加失败");
		}
		return n;
	}

	public int deleteInfoUser(HttpServletRequest req, String info_id) throws InfoUserNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new InfoUserNotFoundException("请重新登录");
		}
		if(info_id==null||info_id.trim().isEmpty()){
			throw new InfoUserNotFoundException("请选择删除的提醒时间");
		}
		InfoUser infoUser = new InfoUser();
		infoUser.setInfo_id(info_id);
		infoUser.setOrg_id(org_id);
		int n = infoUserDao.deleteInfoUser(infoUser);
		if(n!=1){
			throw new InfoUserNotFoundException("系统繁忙，删除失败");
		}
		return n;
	}

	public int updateInfoUser(HttpServletRequest req, InfoUser infoUser)
			throws InfoUserNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		String user_id = (String)session.getAttribute("id");
		if(org_id==null){
			throw new InfoUserNotFoundException("请重新登录");
		}
		if(infoUser==null){
			throw new InfoUserNotFoundException("请填写信息");
		}
		
		if(infoUser.getUser_id()==null||infoUser.getUser_id().trim().isEmpty()){
			throw new InfoUserNotFoundException("请选择用户");
		}
		if(infoUser.getUser_name()==null||infoUser.getUser_name().trim().isEmpty()){
			throw new InfoUserNotFoundException("请选择用户名称");
		}
		if(infoUser.getInfo_flag()==null||infoUser.getInfo_flag().trim().isEmpty()){
			throw new InfoUserNotFoundException("请选择提醒类别");
		}
		if(infoUser.getInfo_title()==null||infoUser.getInfo_title().trim().isEmpty()){
			throw new InfoUserNotFoundException("请选择提醒标题");
		}
		if(infoUser.getWarning_des()==null||infoUser.getWarning_des().trim().isEmpty()){
			throw new InfoUserNotFoundException("请填写到期提醒内容");
		}
		InfoUserParam param = new InfoUserParam();
		param.setInfo_id(infoUser.getInfo_id());
		param.setOrg_id(org_id);
		InfoUser oldInfoUser = infoUserDao.findInfoUserById(param);
		if(oldInfoUser==null){
			throw new InfoUserNotFoundException("系统繁忙，修改失败");
		}
		
		InfoUser newInfoUser = new InfoUser();
		if(!infoUser.getUser_id().equals(oldInfoUser.getUser_id())){
			newInfoUser.setUser_id(infoUser.getUser_id());
		}
		if(!infoUser.getUser_name().equals(oldInfoUser.getUser_name())){
			newInfoUser.setUser_name(infoUser.getUser_name());
		}
		if(!infoUser.getInfo_flag().equals(oldInfoUser.getInfo_flag())){
			newInfoUser.setInfo_flag(infoUser.getInfo_flag());
		}
		if(!infoUser.getInfo_title().equals(oldInfoUser.getInfo_title())){
			newInfoUser.setInfo_title(infoUser.getInfo_title());
		}
		if(!infoUser.getWarning_des().equals(oldInfoUser.getWarning_des())){
			newInfoUser.setWarning_des(infoUser.getWarning_des());
		}
		if(infoUser.getState()!=null){
			if(!infoUser.getState().equals(oldInfoUser.getState())){
				newInfoUser.setState(infoUser.getState());
			}
		}	
		
		newInfoUser.setOpt_user_id(user_id);		
		newInfoUser.setInfo_id(infoUser.getInfo_id());
		newInfoUser.setOrg_id(org_id);
		int n = infoUserDao.updateInfoUser(newInfoUser);
		if(n!=1){
			throw new InfoUserNotFoundException("系统繁忙，稍后再试");
		}
		return n;
	}

	public InfoUser findInfoUserById(HttpServletRequest req, String info_id)
			throws InfoUserNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new InfoUserNotFoundException("请重新登录");
		}
		if(info_id==null||info_id.trim().isEmpty()){
			throw new InfoUserNotFoundException("提醒时间不存在");
		}
		InfoUserParam param = new InfoUserParam();
		param.setInfo_id(info_id);
		param.setOrg_id(org_id);
		InfoUser infoUser = infoUserDao.findInfoUserById(param);
		if(infoUser==null){
			throw new InfoUserNotFoundException("系统繁忙，查询失败");
		}
		return infoUser;
	}

	public List<InfoUser> findInfoUserByParam(HttpServletRequest req,InfoUserParam param)
			throws InfoUserNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new InfoUserNotFoundException("请重新登录");
		}
		Integer page = param.getPage();
		Integer limit = param.getLimit();
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}		
		param.setStart(start);	
		param.setOrg_id(org_id);
		List<InfoUser> list = infoUserDao.findInfoUserByParam(param);
		return list;
	}

	public String countInfoUserByParam(HttpServletRequest req,InfoUserParam param)
			throws InfoUserNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new InfoUserNotFoundException("请重新登录");
		}
		param.setOrg_id(org_id);
		String count = infoUserDao.countInfoUserByParam(param);
		return count;
	}

	
}
